﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Job.Web.Framework;
using System.Data.SqlClient;
using System.Data;
using Job.Data.Interfaces;
using System.Collections;
using Job.Data;
using Job.Entity;

namespace Job.Logic.Common
{
    public class CommonLogic
    {
        private readonly ICommon commonDao = DataAccess.commonDao;

        public bool Login(string userName, string password)
        {
            return commonDao.Login(userName, password);
        }
//        public DataSet GetDownsInfo(int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDirection, ArrayList paramList)
//        {
//            string sql = @"select a.file_id,a.file_name,a.file_size/1024 as file_size,a.create_date,a.client_file_name,
//                            b.description,b.downs_count,b.category_id,b.title,b.thumb_image_id,d.name,d.email
//                            from fy_file a,fy_downs b,fy_item_file c,membershipuser d
//                            where b.item_id = c.item_id
//                            and a.file_id = c.file_id
//                            and d.id = b.create_by ";

//            foreach (SqlParameter param in paramList)
//            {
//                switch (param.ParameterName)
//                {
//                    case "module_id":
//                        sql += " and c.module_id = " + param.Value + "";
//                        break;
//                    case "category_id":
//                        sql += " and (b.category_id = " + param.Value + " or 0 = " + param.Value + ")";
//                        break;
//                }
//            }

//            DataSet ds = GetPageDataBySql(sql, pageSize, pageIndex, countType, sortField, sortDirection, paramList);
//            return ds;
//        }


        //private DataSet GetData(int type)
        //{
        //    ArrayList param = new ArrayList();

        //    int cType = 0;
        //    if (ddlCategory.SelectedIndex >= 0)
        //    {
        //        cType = Convert.ToInt32(((ListItem)ddlCategory.Items[ddlCategory.SelectedIndex]).Value);
        //    }

        //    //int cstatusid = -1;
        //    //if (cmbStatus.SelectedIndex > 0)
        //    //{
        //    //    cstatusid = Convert.ToInt32(((ListItem)cmbStatus.Items[cmbStatus.SelectedIndex]).Value);
        //    //}

        //    //string createDateFrom = dtpCreateDateFrom.Text;
        //    //string createDateTo = dtpCreateDateTo.Text;
        //    //string createDate = createDateFrom + "|" + createDateTo;

        //    if (cType >= 0)
        //    {
        //        SqlParameter pm = new SqlParameter();
        //        pm.ParameterName = "category_id";
        //        pm.Value = cType;
        //        pm.SqlDbType = SqlDbType.Int;
        //        param.Add(pm);
        //    }
        //    if (this.ModuleId > 0)
        //    {
        //        SqlParameter pm = new SqlParameter();
        //        pm.ParameterName = "module_id";
        //        pm.Value = this.ModuleId;
        //        pm.SqlDbType = SqlDbType.Int;
        //        param.Add(pm);
        //    }

        //    DataSet ds = facade.GetDownsInfo(pagerButtom.PageSize, pagerButtom.CurrentPageIndex, type, this.SortField, this.SortDirection, param);
        //    return ds;
        //}
        //public void Insert()
        //{
        //    using (TransactionScope ts = new TransactionScope())
        //    {
        //        //factory.UsersDao.CreateUser(user,app);
        //        IUserDao ud = container["User"] as IUserDao;
        //        IApplicationUserDao aud = container["appUser"] as IApplicationUserDao;

        //        User result = ud.CreateUser(user);
        //        ApplicationUser appUser = new ApplicationUser(app, result);
        //        aud.CreateApplicationUser(appUser);
        //        //factory.AppUserDao.CreateApplicationUser(appUser);
        //        ts.Complete();
        //    }
        //}

        public DataSet GetPageDataBySql(string sql, int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDire, ArrayList queryParam)
        {
            string strOrder1 = " order by " + sortField;
            string strOrder2 = " order by " + sortField;
            strOrder1 += (sortDire == SortDirection.Descending) ? " DESC" : " ASC";
            strOrder2 += (sortDire == SortDirection.Descending) ? " ASC" : " DESC";
            if (queryParam != null)
            {
                for (int i = 0; i < queryParam.Count; i++)
                {
                    string fieldName = ((SqlParameter)queryParam[i]).ParameterName;
                    string fieldValue = ((SqlParameter)queryParam[i]).Value.ToString();
                    sql = sql.Replace("@" + fieldName, fieldValue);
                }
            }

            IDataParameter[] arrPara = new IDataParameter[6];
            arrPara[0] = new SqlParameter("@query_str", DbType.String);
            arrPara[0].Value = sql;

            arrPara[1] = new SqlParameter("@order_str1", DbType.String);
            arrPara[1].Value = strOrder1;

            arrPara[2] = new SqlParameter("@order_str2", DbType.String);
            arrPara[2].Value = strOrder2;

            arrPara[3] = new SqlParameter("@page_size", DbType.Int32);
            arrPara[3].Value = pageSize;

            arrPara[4] = new SqlParameter("@page_index", DbType.Int32);
            arrPara[4].Value = pageIndex;

            arrPara[5] = new SqlParameter("@count_type", DbType.Int32);
            arrPara[5].Value = countType;

            DataSet ds = commonDao.GetPageData(arrPara);//sql, strOrder1, strOrder2, pageSize, pageIndex, countType);
            return ds;
        }

        public DataSet GetPageDataBySqlTwoFieldOrder(string sql, int pageSize, int pageIndex, int countType, string sortField, SortDirection sortDire, ArrayList queryParam)
        {
            string strOrder1 = " order by " + sortField;
            string strOrder2 = " order by " + sortField;
            //strOrder1 += (sortDire == SortDirection.Descending) ? " DESC" : " ASC";
            //strOrder2 += (sortDire == SortDirection.Descending) ? " ASC" : " DESC";
            strOrder1 = string.Format(strOrder1,(sortDire == SortDirection.Descending)?" DESC":" ASC",(sortDire == SortDirection.Descending)?" DESC":" ASC");
            strOrder2 = string.Format(strOrder2,(sortDire == SortDirection.Descending)?" ASC":" DESC",(sortDire == SortDirection.Descending)?" ASC":" DESC");

            if (queryParam != null)
            {
                for (int i = 0; i < queryParam.Count; i++)
                {
                    string fieldName = ((SqlParameter)queryParam[i]).ParameterName;
                    string fieldValue = ((SqlParameter)queryParam[i]).Value.ToString();
                    sql = sql.Replace("@" + fieldName, fieldValue);
                }
            }

            IDataParameter[] arrPara = new IDataParameter[6];
            arrPara[0] = new SqlParameter("@query_str", DbType.String);
            arrPara[0].Value = sql;

            arrPara[1] = new SqlParameter("@order_str1", DbType.String);
            arrPara[1].Value = strOrder1;

            arrPara[2] = new SqlParameter("@order_str2", DbType.String);
            arrPara[2].Value = strOrder2;

            arrPara[3] = new SqlParameter("@page_size", DbType.Int32);
            arrPara[3].Value = pageSize;

            arrPara[4] = new SqlParameter("@page_index", DbType.Int32);
            arrPara[4].Value = pageIndex;

            arrPara[5] = new SqlParameter("@count_type", DbType.Int32);
            arrPara[5].Value = countType;

            DataSet ds = commonDao.GetPageData(arrPara);//sql, strOrder1, strOrder2, pageSize, pageIndex, countType);
            return ds;
        }


        public int GetCountBySql(string sql)
        {
            int Default_Page_Size = 10;
            int Default_Current_Page = 1;
            int Default_Count_Type = 1;
            DataSet ds = GetPageDataBySql(sql, Default_Page_Size, Default_Current_Page, Default_Count_Type,
                "", SortDirection.Descending, null);
            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            
            return count;
        }

        public int GetCountBySqlTwoFieldOrder(string sql)
        {
            int Default_Page_Size = 10;
            int Default_Current_Page = 1;
            int Default_Count_Type = 1;
            DataSet ds = GetPageDataBySqlTwoFieldOrder(sql, Default_Page_Size, Default_Current_Page, Default_Count_Type,
                "", SortDirection.Descending, null);
            int count = Convert.ToInt32(ds.Tables[0].Rows[0][0]);

            return count;
        }


        public DateTime GetServerTime()
        {
            return Convert.ToDateTime( commonDao.GetServerTime());
        }
    }


}
